﻿using Microsoft.WindowsAzure.ServiceRuntime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using Repositories;
using Domain;

namespace Services
{
	public class UserService
	{
		public UserService()
		{
		}

		public void InsertUser(string username, string password)
		{
			UserRepository userRep = new UserRepository();
			userRep.InsertUser(username, password);
		}


		/// <summary>
		/// Updates details of a user in the database. Expect a non-encrypted password.
		/// This method will add a salt to the password and encrypt it with  MD5.
		/// /// </summary>
		/// <param name="username">Username of the user that will be updated </param>
		/// <param name="password">Non-encrypted password</param>
		public void UpdateUser(string username, string password)
		{
			//The password of a user is salted and then encrypted with MD5
			//First: get the salt. It is stored in the ServiceConfiguration.cscfg file
			string passwordSalt = RoleEnvironment.GetConfigurationSettingValue("PasswordSalt");

			string hashedPW = FormsAuthentication.HashPasswordForStoringInConfigFile(password + passwordSalt, "MD5");

			UserRepository userRep = new UserRepository();
			userRep.UpdateUser(username, hashedPW);
		}

		public User getUserDetails(string username)
		{
			UserRepository userRep = new UserRepository();
			return userRep.getUser(username);
		}

	}
}
